<template>
  <div class="app-container">
    <!-- 左侧导航 -->
    <div class="sidebar-container">
      <div class="logo-box">
        <img src="@/assets/logo.png" class="logo-img" alt="logo">
        <span class="logo-text">景区管理系统</span>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        unique-opened>
        
        <el-menu-item index="1">
          <i class="el-icon-location"></i>
          <span>景点管理</span>
        </el-menu-item>
        
        <el-menu-item index="2" @click="tohuodon">
          <i class="el-icon-ticket"></i>
          <span>活动管理</span>
        </el-menu-item>
      </el-menu>
    </div>

    <!-- 主要内容区 -->
    <div class="main-container">
      <!-- 顶部导航 -->
      <div class="navbar">
        <div class="left-menu">
          <i class="el-icon-s-fold"></i>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>景点管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="right-menu">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              管理员 <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>

      <!-- 内容区域 -->
      <div class="app-main">
        <!-- 搜索区域 -->
        <div class="search-container">
          <el-card shadow="never">
            <el-form :inline="true" :model="formInline" class="search-form">
              <el-form-item label="景点名称">
                <el-input
                  v-model="formInline.nameMark"
                  placeholder="请输入景点名称"
                  prefix-icon="el-icon-search"
                  clearable>
                </el-input>
              </el-form-item>
              
              <el-form-item label="推荐季节">
                <el-select 
                  v-model="formInline.playSeason"
                  placeholder="请选择推荐游玩季节"
                  clearable>
                  <el-option label="全适合" value="全适合"></el-option>
                  <el-option label="春天" value="春天"></el-option>
                  <el-option label="夏天" value="夏天"></el-option>
                  <el-option label="秋天" value="秋天"></el-option>
                  <el-option label="冬天" value="冬天"></el-option>
                </el-select>
              </el-form-item>
              
              <el-form-item>
                <el-button type="primary" @click="findPage" icon="el-icon-search">
                  查询
                </el-button>
                <el-button @click="resetForm">
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </div>

        <!-- 表格区域 -->
        <div class="table-container">
          <el-card shadow="never">
            <div slot="header" class="clearfix">
              <span class="card-title">景点列表</span>
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="toAdd()"
                class="float-right">
                添加景点
              </el-button>
            </div>

            <el-table
              v-loading="tableLoading"
              :data="tableData"
              border
              stripe
              style="width: 100%"
              height="calc(100vh - 380px)">
              
              <el-table-column
                type="index"
                label="序号"
                width="50"
                align="center">
              </el-table-column>
              
              <el-table-column
                prop="nameMark"
                label="景点名称"
                min-width="120">
              </el-table-column>
              
              <el-table-column
                prop="coverUrl"
                label="封面图片"
                width="100"
                align="center">
                <template slot-scope="scope">
                  <el-image
                    :src="scope.row.coverUrl"
                    fit="cover"
                    style="width: 60px; height: 60px; border-radius: 4px;"
                    :preview-src-list="[scope.row.coverUrl]">
                    <div slot="error" class="image-slot">
                      <i class="el-icon-picture-outline"></i>
                    </div>
                  </el-image>
                </template>
              </el-table-column>

              <el-table-column
                prop="playSeason"
                label="推荐季节"
                width="100"
                align="center">
                <template slot-scope="scope">
                  <el-tag :type="getSeasonTagType(scope.row.playSeason)">
                    {{ scope.row.playSeason }}
                  </el-tag>
                </template>
              </el-table-column>

              <el-table-column
                prop="intro"
                label="简介"
                show-overflow-tooltip>
              </el-table-column>

              <el-table-column
                label="操作"
                width="150"
                align="center">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    @click="handleEdit(scope.row)">
                    编辑
                  </el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination-container">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
              </el-pagination>
            </div>
          </el-card>
        </div>
      </div>
    </div>

    <!-- 添加/编辑弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      width="50%"
      :close-on-click-modal="false">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="dialog-form">
        
        <el-form-item label="景点名称" prop="nameMark">
          <el-input v-model="form.nameMark" placeholder="请输入景点名称"></el-input>
        </el-form-item>

        <el-form-item label="景点图片" prop="coverUrl">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:7777/api-product/oss/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.coverUrl" :src="form.coverUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <div class="upload-tip">支持 jpg 格式，大小不超过 2MB</div>
        </el-form-item>

        <el-form-item label="推荐季节" prop="playSeason">
          <el-select v-model="form.playSeason" placeholder="请选择推荐游玩季节">
            <el-option label="全适合" value="全适合"></el-option>
            <el-option label="春天" value="春天"></el-option>
            <el-option label="夏天" value="夏天"></el-option>
            <el-option label="秋天" value="秋天"></el-option>
            <el-option label="冬天" value="冬天"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="简介" prop="intro">
          <el-input
            type="textarea"
            v-model="form.intro"
            :rows="4"
            placeholder="请输入景点简介">
          </el-input>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'ScenicManagement',
  data() {
    return {
      activeMenu: '1',
      tableLoading: false,
      dialogTitle: '添加景点',
      dialogFormVisible: false,
      formInline: {
        nameMark: '',
        playSeason: ''
      },
      form: {
        nameMark: '',
        coverUrl: '',
        playSeason: '',
        intro: ''
      },
      rules: {
        nameMark: [
          { required: true, message: '请输入景点名称', trigger: 'blur' }
        ],
        // coverUrl: [
        //   { required: true, message: '请上传景点图片', trigger: 'change' }
        // ],
        playSeason: [
          { required: true, message: '请选择推荐季节', trigger: 'change' }
        ],
        intro: [
          { required: true, message: '请输入景点简介', trigger: 'blur' }
        ]
      },
      tableData: [],
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.findPage()
  },
  methods: {
    toAdd(){
       this.form = {};
       this.dialogFormVisible = true
    },
    getSeasonTagType(season) {
      const types = {
        '春天': 'success',
        '夏天': 'danger',
        '秋天': 'warning',
        '冬天': 'info',
        '全适合': 'primary'
      }
      return types[season] || ''
    },
    
    resetForm() {
      this.formInline = {
        nameMark: '',
        playSeason: ''
      }
      this.findPage()
    },

    findPage() {
      this.tableLoading = true
      axios.post(`http://localhost:7777/api-product/scenic/findPage?page=${this.page}&pageSize=${this.size}`, 
        this.formInline
      ).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },

    handleEdit(row) {
      this.dialogTitle = '编辑景点'
      this.form = { ...row }
      this.dialogFormVisible = true
    },

    handleDelete(row) {
      this.$confirm('确认删除该景点?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('删除成功')
        this.findPage()
      })
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          axios.post('http://localhost:7777/api-product/scenic/add', this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success('操作成功')
              this.dialogFormVisible = false
              this.findPage()
              this.form = {}
            } else {
              this.$message.error(res.data.msg || '操作失败')
            }
          })
        }
      })
    },

    handleSizeChange(val) {
      this.size = val
      this.findPage()
    },

    handleCurrentChange(val) {
      this.page = val
      this.findPage()
    },

    handleAvatarSuccess(res) {
      this.form.coverUrl = res.data
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    tohuodon() {
      this.$router.push('/huodon')
    }
  }
}
</script>

<style>
.app-container {
  height: 100vh;
  display: flex;
}

.sidebar-container {
  width: 210px;
  height: 100%;
  background: #304156;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  overflow-y: auto;
}

.logo-box {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  background: #2b2f3a;
}

.logo-img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.logo-text {
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.sidebar-menu {
  border: none;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 210px;
}

.main-container {
  flex: 1;
  margin-left: 210px;
  display: flex;
  flex-direction: column;
  background: #f0f2f5;
  min-height: 100vh;
}

.navbar {
  height: 50px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.left-menu {
  display: flex;
  align-items: center;
}

.left-menu i {
  font-size: 20px;
  cursor: pointer;
  margin-right: 20px;
}

.right-menu .el-dropdown-link {
  cursor: pointer;
  color: #666;
}

.right-menu .el-dropdown-link:hover {
  color: #409EFF;
}

.app-main {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.search-container {
  margin-bottom: 20px;
}

.search-container .el-card {
  margin-bottom: 0;
}

.table-container .card-title {
  font-size: 16px;
  font-weight: 500;
}

.table-container .float-right {
  float: right;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}

.avatar {
  width: 148px;
  height: 148px;
  display: block;
  object-fit: cover;
}

.upload-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 8px;
}

.pagination-container {
  padding: 20px 0;
  text-align: right;
}

@media screen and (max-width: 1200px) {
  .sidebar-container {
    width: 64px;
  }
  
  .logo-text {
    display: none;
  }
  
  .main-container {
    margin-left: 64px;
  }
}
</style>